<div class="smart-rightPanel layui-form">
    <div class="layui-anim layui-anim-upbit">
        <blockquote class="site-text layui-elem-quote">
            <div class="layui-form-item wx-search" id="list_form">
                <div class="layui-inline">
                    <input type="text" name="name" placeholder="请输入任务名称" autocomplete="off" class="layui-input">
                    <input type="hidden" name="field" id="field">
                    <input type="hidden" name="order" id="order">
                    <input type="hidden" name="isclose" id="isclose">
                    <input type="hidden" name="taskStatus" id="taskStatus">
                    <input type="hidden" name="code" id="code">
                </div>
                <button class="layui-btn btn-primary" id="search">搜索</button>
                <div class="layui-inline right">
                    <button class="layui-btn btn-primary" id="save">新建任务</button>
                    <button class="layui-btn btn-primary" id="moban">导入模板</button>
                    <button class="layui-btn btn-danger" id="del">批量删除</button>
                </div>
            </div>
            <div class="layui-inline">
                <button class="layui-btn btn-primary" id="aa">投放中(<span id="a"></span>)</button>
                <button class="layui-btn btn-danger" id="bb">暂停(<span id="b"></span>)</button>
                <button class="layui-btn btn-primary" id="cc">待对比(<span id="c"></span>)</button>
                <button class="layui-btn btn-danger" id="dd">对比完成(<span id="d"></span>)</button>
            </div>
        </blockquote>
        <table id="list_table" class="layui-hide" lay-filter="mainList"></table>
    </div>
</div>
<script>
    layui.use(['layer', 'table', 'form','jquery','layedit'], function () {
        var layedit = layui.layedit;
        var layer = layui.layer;
        var table = layui.table;
        var form = layui.form;
        var  $ = layui.jquery;
        var tableObj = table.render({
            id: 'list_table'
            , elem: '#list_table'
            , url: 'buss/task'
            , where: {id:getHashParam().id}
            , align: "center"
            , cols: [[ //表头
                {type: 'checkbox'}
                , {field: 'lock', title: '操作', width: 420, templet: function (d) {
                    var zhi="";
                    if(!d.stick){
                        zhi= '<button class="layui-btn btn-primary layui-btn-sm" lay-event="stick">设置置顶</button>';
                    }else {
                        zhi= '<button class="layui-btn btn-danger layui-btn-sm" lay-event="stick">取消置顶</button>';
                    }
                    var taskStatus="";
                    if(d.taskStatus==0){
                        taskStatus= '<button class="layui-btn btn-primary layui-btn-sm" lay-event="stop">投放</button>';
                    }else {
                        taskStatus= '<button class="layui-btn btn-danger layui-btn-sm" lay-event="stop">暂停</button>';
                    }

                    var hide="";
                    if(d.ishide==0){
                        hide= '<button class="layui-btn btn-primary layui-btn-sm" lay-event="hide">隐藏</button>';
                    }else {
                        hide= '<button class="layui-btn btn-danger layui-btn-sm" lay-event="hide">显示</button>';
                    }
                        return hide+zhi+taskStatus
                            +'<button class="layui-btn layui-bg-green layui-btn-sm" lay-event="edit">修改</button>'
                            +'<button class="layui-btn layui-btn-warm layui-btn-sm" lay-event="excel">导出</button>'
                            +'<button class="layui-btn layui-bg-blue layui-btn-sm" lay-event="daoru">导入对比</button>';
                    }, unresize: true, align: 'center'}
                , {field: 'id', title: '编号',align: 'center',hide:true}
                , {field: 'name', title: '任务名', width: 140, align: 'center'}
                , {field: 'imgIcon', title: '任务图标', width: 140, align: 'center', templet: function (d) {
                        return '<div><img src="'+d.imgIcon+'" style="width: 40px;height: 40px"></div>';
                    }}
                , {field: 'taskStatus', title: '任务状态', width: 140, align: 'center'
                    , templet: function (d) {
                        return d.taskStatus==1?'已上架':'未上架';
                    }}
                , {field: 'fnum', title: '份数', width: 140, align: 'center'}
                , {field: 'price', title: '单价', width: 140, align: 'center'}
                , {field: 'userPrice', title: '用户奖励', width: 140, align: 'center'}
                , {field: 'startNum', title: '开始人数', width: 140, align: 'center'}
                , {field: 'subNum', title: '提交人数', width: 140, align: 'center'}
                , {field: 'doneFnum', title: '完成份数', width: 140, align: 'center'}
                , {field: 'imgShili', title: '示例图片', width: 140, align: 'center', templet: function (d) {
                        if(!d.imgShili){
                            return '';
                        }
                        var arr=d.imgShili.split(',')
                        return '<div><img src="'+arr[0]+'" style="width: 40px;height: 40px"></div>';
                    }}
                , {field: 'skipHerf', title: '跳转链接', width: 140, align: 'center'}
                , {field: 'timeOut', title: '超时时间', width: 140, align: 'center'}

            ]]
            , done :function(res,curr,count){
                $(".layui-table-box").find("[data-field='id']").css("display","none");
            }
        });

        $("#search").click(function () {
            reloadTable(tableObj);
        });

        $("#save").click(function () {
            showDialog({
                title: '添加'
                , template: 'add'
                , saveUrl: 'buss/task/save'
                , tableObj: tableObj
                , width: 850
                , height: 650
                , success:function () {

                    layedit.set({
                        //暴露layupload参数设置接口 --详细查看layupload参数说明

                        codeConfig: {
                            hide: false,  //是否显示编码语言选择框
                            default: 'javascript' //hide为true时的默认语言格式
                        }
                        //新增iframe外置样式和js
                        , quote:{
                            style: ['./js/plugin/layui/src/css/layui.css','./others'],
                            js: ['./js/plugin/jquery-2.2.4.min.js']
                        }
                        //fontFomatt:["p","span"]  //自定义段落格式 ，如不填，默认为 ["p", "h1", "h2", "h3", "h4", "h5", "h6", "div"]~~
                        , tool: [
                            'html', 'undo', 'redo', 'code', 'strong', 'italic', 'underline', 'del', 'addhr', '|', 'fontFomatt','fontfamily', 'fontBackColor', 'colorpicker'
                            , '|', 'left', 'center', 'right', '|', 'link', 'unlink', 'anchors'
                            , '|'
                            , 'table','customlink'
                            , 'fullScreen'
                        ]
                    });

                    layeditIndex = layedit.build('remarks');
                }
                ,beforeSubmit:function (data) {

                    data.remarks=layedit.getContent(layeditIndex);
                    if(!data.remarks){
                        layer.msg("请填写内容!");
                        return false;
                    }
                    if (data.taskStatus=='-1') {
                        layer.msg("请选择状态");
                        return false;
                    }
                    var qudao="";
                    for(var item in data){
                        if(item.indexOf("qudao") == 0 && data[item].indexOf("on")>-1){
                            qudao+=item.replace("qudao")+",";
                        }
                    }
                    data.qudao=qudao;
                    if (data.qudao=='') {
                        layer.msg("请选择渠道");
                        return false;
                    }
                    layer.msg(data.qudao);
                    return true;
                }
            })
        });

        //表格内部操作按钮监听
        table.on('tool(mainList)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') {
                showDialog({
                    title: '修改'
                    , template: 'edit'
                    , saveUrl: 'buss/task/save'
                    , tableObj: tableObj
                    , width: 850
                    , height: 650
                    , htmlData: data
                    , success:function () {
                        layedit.set({
                            codeConfig: {
                                hide: false,  //是否显示编码语言选择框
                                default: 'javascript' //hide为true时的默认语言格式
                            }
                            //新增iframe外置样式和js
                            , quote:{
                                style: ['./js/plugin/layui/src/css/layui.css','./others'],
                                js: ['./js/plugin/jquery-2.2.4.min.js']
                            }
                            //fontFomatt:["p","span"]  //自定义段落格式 ，如不填，默认为 ["p", "h1", "h2", "h3", "h4", "h5", "h6", "div"]~~
                            , tool: [
                                'html', 'undo', 'redo', 'code', 'strong', 'italic', 'underline', 'del', 'addhr', '|', 'fontFomatt','fontfamily', 'fontBackColor', 'colorpicker'
                                , '|', 'left', 'center', 'right', '|', 'link', 'unlink', 'anchors'
                                , '|'
                                , 'table','customlink'
                                , 'fullScreen'
                            ]
                        });

                        layeditIndex = layedit.build('remarks');
                    }
                    ,beforeSubmit:function (data) {

                        data.remarks=layedit.getContent(layeditIndex);
                        if(!data.remarks){
                            layer.msg("请填写内容!");
                            return false;
                        }
                        if (data.taskStatus=='-1') {
                            layer.msg("请选择状态");
                            return false;
                        }
                        var qudao="";
                        for(var item in data){
                            if(item.indexOf("qudao") == 0 && data[item].indexOf("on")>-1){
                                qudao+=item.replace("qudao")+",";
                            }
                        }
                        data.qudao=qudao;
                        if (data.qudao=='') {
                            layer.msg("请选择渠道");
                            return false;
                        }
                        return true;
                    }
                })


            }else if(layEvent === 'excel'){
                window.location.href="buss/task/excel?taskId="+data.id;

            }else if(layEvent === 'stick'){
               $("#field").val(data.id);
                reloadTable(tableObj);
                $("#field").val(null);

            }else if(layEvent === 'hide'){
                $.ajax({
                    type: "GET",
                    url: 'buss/task/hide?id='+data.id,
                    success: function (result) {
                        if (result.success) {
                            reloadTable(tableObj);
                        }
                    }
                })

            }else if(layEvent === 'stop'){
                $("#order").val(data.id);
                reloadTable(tableObj);
                $("#order").val(null);
            }else if(layEvent === 'daoru'){
                showDialog({
                    title: '比对录入',
                    template: 'daoru',
                    saveUrl: 'buss/task/log/out',
                    tableObj: tableObj,
                    htmlData: data,
                    width : 750
                })
            }

        });


        $("#moban").click(function () {
            window.location.href="buss/task/moban";
        });

        $("#aa").click(function () {
            $("#taskStatus").val(1);
            reloadTable(tableObj);
            $("#taskStatus").val(null);
        });

        $("#bb").click(function () {
            $("#taskStatus").val(0);
            reloadTable(tableObj);
            $("#taskStatus").val(null);
        });

        $("#cc").click(function () {
            $("#code").val("duibi");
            $("#isclose").val(1);
            reloadTable(tableObj);
            $("#code").val(null);
            $("#isclose").val(null);
        });

        $("#dd").click(function () {
            $("#code").val("wancheng");
            $("#isclose").val(1);
            reloadTable(tableObj);
            $("#code").val(null);
            $("#isclose").val(null);
        });




        // 删除
        $("#del").click(function () {
            var data = table.checkStatus('list_table').data;//已选中数据
            if (data.length == 0) {
                layer.msg("至少选择一条");
                return;
            }
            var arr = [];     //选中数组
            for (var i = 0; i < data.length; i++) {
                arr.push(data[i].id)
            }
            showConfirm("确认删除？", function () {
                $.ajax({
                    url: 'buss/task/del',
                    data: {"id": arr.join(",")},
                    success: function (result) {
                        if (result.success) {
                            layer.msg("删除成功");
                            reloadTable(tableObj);
                        } else {
                            layer.msg("删除失败");
                        }
                    }
                })
            });
        });

        tianchong();
       function tianchong() {
           $.ajax({
               type: "GET",
               url: 'buss/task/info',
               success: function (result) {
                   if (result.success) {
                       $("#a").html(result.data.a);
                       $("#b").html(result.data.b);
                       $("#c").html(result.data.c);
                       $("#d").html(result.data.d);
                   }
               }
           })
       }


    });
</script>

